package com.lc.zpyh.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.lifecycle.ApplicationLifecycle;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.ToastUtils;
import com.lc.zpyh.R;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.request.ReceiveCouponsApi;
import com.lc.zpyh.http.request.SelectCOunpByCanteenIdNewApi;
import com.lc.zpyh.http.response.SelectCOunpByCanteenIdNewBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.util.BigDecimalUtils;
import com.lc.zpyh.util.SPUtil;
import com.lc.zpyh.widget.CollectCouponsDialog;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.ButterKnife;

public class CollectCouponsNewDialog extends Dialog implements View.OnClickListener{

    private Context context;
    private ImageView img_close;
    private TextView tv_title;
    private RecyclerView rv_coupon;

    List<SelectCOunpByCanteenIdNewBean.CouponsBean> canteeList=new ArrayList<>();

    private CollectCouponsDialog.OnGoodsOrderListener onGoodsOrderListener;
    private BaseQuickAdapter<SelectCOunpByCanteenIdNewBean.CouponsBean, BaseViewHolder> couponAdapter;


    private String title;
    private String merchantid;
    public CollectCouponsNewDialog(Context context, List<SelectCOunpByCanteenIdNewBean.CouponsBean> canteeList, String title,String merchantid) {
        super(context, R.style.dialog);
        this.context = context;
        this.canteeList = canteeList;
        this.title = title;
        this.merchantid = merchantid;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        Window window = getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
//        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
        setContentView(R.layout.dialog_collectnewcoupons);
        WindowManager windowManager = ((Activity) context).getWindowManager();
//        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.width = (int) (d.widthPixels * 0.85);
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);// 点击Dialog外部消失

        initView();


    }
    private void initView() {
        tv_title= findViewById(R.id.tv_title);
        rv_coupon = findViewById(R.id.rv_coupon);
        img_close = findViewById(R.id.img_close);

        img_close.setOnClickListener(this);
       tv_title.setText(title);
        coupon(title);


    }





    private void coupon(String title) {


                    couponAdapter = new BaseQuickAdapter<SelectCOunpByCanteenIdNewBean.CouponsBean,
                            BaseViewHolder>(R.layout.item_newcoupon, canteeList) {

                        @Override
                        protected void convert(BaseViewHolder baseViewHolder,SelectCOunpByCanteenIdNewBean.CouponsBean dataBean) {

                            baseViewHolder.setText(R.id.tv_money, BigDecimalUtils.div(dataBean.getCreditFor()+"","100",2));

                            baseViewHolder.setText(R.id.tv_address,title);

                            baseViewHolder.setText(R.id.tv_date,Html.fromHtml("满"+BigDecimalUtils.div(dataBean.getMeetTheConditions()+"","100",0)+"可用 <br>有效天数："+dataBean.getCouponEffectiveTime()+"天<br/>"));
                  if (dataBean.getGetSart()==0){
                      baseViewHolder.setVisible(R.id.tv_lingqu,true);
                      baseViewHolder.setGone(R.id.ll_to_make,true);
                  }else {
                      baseViewHolder.setGone(R.id.tv_lingqu,true);
                      baseViewHolder.setVisible(R.id.ll_to_make,true);
                  }
                          baseViewHolder.getView(R.id.ll_to_make).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    dismiss();
                                }
                            });

                            baseViewHolder.getView(R.id.tv_lingqu).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                 lingqu(dataBean.getCouponId()+"");
                                }
                            });

                        }


                    };

                    View emptyview = View.inflate(context, R.layout.empty_emptylayout, null);

                    couponAdapter.setEmptyView(emptyview);

                    rv_coupon.setAdapter(couponAdapter);




    }

    private void lingqu(String couponId) {
        EasyHttp.post(new ApplicationLifecycle())
                .api(new ReceiveCouponsApi().setCouponId(couponId).setUserId(SPUtil.get(context, IntentKey.USERID, ""))).request(new OnHttpListener<PublicMsgBean>() {

            @Override
            public void onSucceed(PublicMsgBean data) {
                if (data.getCode()==0) {
                    ToastUtils.show("领取成功");
                    coupon1();
                }else {
                    ToastUtils.show(data.getMsg());
                }
            }

            @Override
            public void onFail(Exception e) {


            }
        });
    }
    private void coupon1() {
        EasyHttp.post(new ApplicationLifecycle())
                .api(new SelectCOunpByCanteenIdNewApi().setId(merchantid).setPageNum("1").setPageSize("999").setType("0").setUserId(SPUtil.get(context, IntentKey.USERID, ""))).request(new OnHttpListener<SelectCOunpByCanteenIdNewBean>() {

            @Override
            public void onSucceed(SelectCOunpByCanteenIdNewBean data) {
                if (data.getCoupons().size() > 0) {
                    couponAdapter.setNewData(data.getCoupons());
                    couponAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFail(Exception e) {


            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.img_close:


                dismiss();
                break;



            default:
                break;
        }
    }
    public interface OnGoodsOrderListener {
        void onDismiss( String time);


        void onBuyNow( String time);


    }

    public void setOnGoodsOrderListener(CollectCouponsDialog.OnGoodsOrderListener onGoodsOrderListener) {
        this.onGoodsOrderListener = onGoodsOrderListener;
    }
}
